import { userList } from './data'
import { getNowDateTime } from '../../utils'
export default [
  {
    url: '/system/user/profile/update', // 更新基础信息
    method: 'put',
    response: data => {
      updateBasicInfo(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/user/profile/update-password', // 更新密码
    method: 'put',
    response: data => {
      updatePassword(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/user/profile/update-avatar', // 更新头像
    method: 'post',
    response: data => {
      updateAvatar(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/user/profile/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  }
]

const updateBasicInfo = data => {
  userList.forEach(item => {
    if (item.id === 1) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const updatePassword = data => {
  userList.forEach(item => {
    if (item.id === 1) {
      item.password = data.body.newPassword
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const updateAvatar = data => {
  userList.forEach(item => {
    if (item.id === data.body.id) {
      item.avatar = data.body.avatar
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const getInfoById = params => {
  const result = userList.filter(item => {
    return item.id === 1
  })
  if (result.length > 0) {
    const res = result[0]
    res.dept = {
      id: 1,
      name: '总公司'
    }
    res.roles = [{ id: 1, name: '超级管理员' }]
    res.posts = [{ id: 1, name: '总经理' }]
    return result[0]
  } else {
    return null
  }
}
